package com.chengh.gulimall.ware.exception;

import com.chengh.common.exception.BizCodeEnume;
import com.chengh.common.exception.MyException;
import com.chengh.common.exception.RRException;
import com.chengh.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * 集中处理所有异常
 */
@Slf4j
@RestControllerAdvice(basePackages="com.chengh.gulimall.ware")
public class GulimallExceptionControllerAdvice {

    /**
     * 处理没有指定的异常,这样程序即使发现异常,也能在这儿做处理,前端页面友好
     * @return
     */
    @ExceptionHandler(value = Throwable.class)
    public  R handleException(Throwable throwable) {
        log.error("错误: ",throwable);
        return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(),BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
    }

    /**
     * 此处返回自定义的异常信息
     * @param e 自己定义的异常类
     * @return
     */
    @ExceptionHandler(value = MyException.class)
    public R MyException(MyException e) {
        System.out.println("获得了异常");
        System.out.println(e.getMsg());
        return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(),e.getMsg());
    }
}
